package com.doubi.vo;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.doubi.model.sys.TSystemNotice;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.io.Serializable;
import java.util.Collections;
import java.util.List;

@ApiModel(value = "分页对象")
public class PageVo<T> implements Serializable, IPage<T> {

    @ApiModelProperty(value = "返回码", name = "code", example = "0",hidden = true)
    private int code;//返回码  0成功

    @ApiModelProperty(value = "总行数", name = "total", example = "0")
    private long total = 0;//page 总行数

    @ApiModelProperty(value = "数据", name = "数据list")
    private List<T> records = Collections.emptyList();//数据

    @ApiModelProperty(value = "错误消息", name = "msg", example = "msg",hidden = true)
    private String msg;//错误消息

    @ApiModelProperty(value = "页码", name = "current", example = "1")
    private long current = 1;//页码

    @ApiModelProperty(value = "行数", name = "limit", example = "0")
    private long limit = 0;//行数


    /**
     * 每页显示条数，默认 10
     */
    @ApiModelProperty(value = "条数", name = "size", example = "10")
    private long size = 10;
    /**
     * SQL 排序 ASC 数组
     */
    @ApiModelProperty(value = "asc排序", name = "ascs", example = "ascs",hidden = true)
    private String[] ascs;
    /**
     * SQL 排序 DESC 数组
     */
    @ApiModelProperty(value = "desc排序", name = "descs", example = "descs",hidden = true)
    private String[] descs;
    /**
     * 自动优化 COUNT SQL
     */
    @ApiModelProperty(value = "optimizeCountSql", name = "自动优化 COUNT SQL", example = "自动优化 COUNT SQL",hidden = true)
    private boolean optimizeCountSql = true;
    /**
     * 是否进行 count 查询
     */
    @ApiModelProperty(value = "isSearchCount", name = "count 查询", example = "count 查询",hidden = true)
    private boolean isSearchCount = true;

    public PageVo() {
        // to do nothing
    }

    /**
     * 分页构造函数
     */
    public PageVo(long current, long size) {
        this(current, size, 0);
    }


    public PageVo(long current, long size, long total) {
        this(current, size, total, true,Collections.emptyList());
    }

    public PageVo(long current, long size, long total,List<T> records) {
        this(current, size, total, true,records);
    }
    public PageVo(long current, long size, boolean isSearchCount) {
        this(current, size, 0, isSearchCount,Collections.emptyList());
    }

    public PageVo(long current, long size, long total, boolean isSearchCount,List<T> records) {
        if (current > 1) {
            this.current = current;
        }
        this.size = size;
        this.total = total;
        this.isSearchCount = isSearchCount;
        this.records = records;
    }

    public PageVo(int code, long current, List<T> records, String msg) {
        this.code = code;
        this.current = current;
        this.records = records;
        this.msg = msg;
    }

    public long getLimit() {
        return limit;
    }

    public void setLimit(long limit) {
        this.limit = limit;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }



    /**
     * 是否存在上一页
     *
     * @return true / false
     */
    public boolean hasPrevious() {
        return this.current > 1;
    }

    /**
     * 是否存在下一页
     *
     * @return true / false
     */
    public boolean hasNext() {
        return this.current < this.getPages();
    }

    @Override
    public List<T> getRecords() {
        return this.records;
    }

    @Override
    public PageVo<T> setRecords(List<T> records) {
        this.records = records;
        return this;
    }

    @Override
    public long getTotal() {
        return this.total;
    }

    @Override
    public PageVo<T> setTotal(long total) {
        this.total = total;
        return this;
    }

    @Override
    public long getSize() {
        return this.size;
    }

    @Override
    public PageVo<T> setSize(long size) {
        this.size = size;
        return this;
    }

    @Override
    public long getCurrent() {
        return this.current;
    }

    @Override
    public PageVo<T> setCurrent(long current) {
        this.current = current;
        return this;
    }

    @Override
    public String[] ascs() {
        return ascs;
    }

    public PageVo<T> setAscs(List<String> ascs) {
        if (CollectionUtils.isNotEmpty(ascs)) {
            this.ascs = ascs.toArray(new String[0]);
        }
        return this;
    }


    /**
     * 升序
     *
     * @param ascs 多个升序字段
     */
    public PageVo<T> setAsc(String... ascs) {
        this.ascs = ascs;
        return this;
    }

    @Override
    public String[] descs() {
        return descs;
    }

    public PageVo<T> setDescs(List<String> descs) {
        if (CollectionUtils.isNotEmpty(descs)) {
            this.descs = descs.toArray(new String[0]);
        }
        return this;
    }

    /**
     * 降序
     *
     * @param descs 多个降序字段
     */
    public PageVo<T> setDesc(String... descs) {
        this.descs = descs;
        return this;
    }

    @Override
    public boolean optimizeCountSql() {
        return optimizeCountSql;
    }

    @Override
    public boolean isSearchCount() {
        if (total < 0) {
            return false;
        }
        return isSearchCount;
    }
    public PageVo<T> setSearchCount(boolean isSearchCount) {
        this.isSearchCount = isSearchCount;
        return this;
    }

    public PageVo<T> setOptimizeCountSql(boolean optimizeCountSql) {
        this.optimizeCountSql = optimizeCountSql;
        return this;
    }
}
